package com.zxwl.common.log;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LocationInfo;

public class LogWriter implements ILogger
{

	public LogWriter(Logger logger)
	{
		this.logger = logger;
	}

	public void debug(Object message)
	{
		if (logger.isDebugEnabled())
			logger.debug((new StringBuilder()).append(message).append(getLocationInfo(null)).toString());
	}

	public String getLocationInfo(Exception e)
	{
		StringBuilder codeInfo = new StringBuilder();
		if (e != null)
		{
			if (e.getStackTrace() != null && e.getStackTrace().length > 0)
			{
				for (int i = 0; i < e.getStackTrace().length; i++)
				{
					StackTraceElement ele = e.getStackTrace()[i];
					if (ele == null || ele.getClassName().indexOf(logger.getName()) < 0)
						continue;
					codeInfo.append("  ");
					codeInfo.append(ele.getClassName());
					codeInfo.append(".");
					codeInfo.append(ele.getMethodName());
					codeInfo.append(" ");
					codeInfo.append("(");
					codeInfo.append(ele.getFileName());
					codeInfo.append(":");
					codeInfo.append(ele.getLineNumber());
					codeInfo.append(")");
					break;
				}

			}
		}
		else
		{
			LocationInfo locationInfo = new LocationInfo(new Throwable(), getClass().getName());
			codeInfo.append("  ");
			codeInfo.append(locationInfo.getClassName());
			codeInfo.append(".");
			codeInfo.append(locationInfo.getMethodName());
			codeInfo.append(" ");
			codeInfo.append("(");
			codeInfo.append(locationInfo.getFileName());
			codeInfo.append(":");
			codeInfo.append(locationInfo.getLineNumber());
			codeInfo.append(")");
		}
		return codeInfo.toString();
	}

	public void info(Object message)
	{
		if (logger.isInfoEnabled())
			logger.info((new StringBuilder()).append(message).append(getLocationInfo(null)).toString());
	}

	public void warn(Object message)
	{
		if (logger.isEnabledFor(Level.WARN))
			logger.warn((new StringBuilder()).append(message).append(getLocationInfo(null)).toString());
	}

	public void error(Object message)
	{
		if (logger.isEnabledFor(Level.ERROR))
			logger.error((new StringBuilder()).append(message).append(getLocationInfo(null)).toString());
	}

	public void fatal(Object message)
	{
		if (logger.isEnabledFor(Level.FATAL))
			logger.fatal((new StringBuilder()).append(message).append(getLocationInfo(null)).toString());
	}

	public void debug(Object message, Exception e)
	{
		if (logger.isDebugEnabled())
		{
			StringBuilder messageBuffer = new StringBuilder();
			messageBuffer.append(message);
			messageBuffer.append(getLocationInfo(e));
			messageBuffer.append("\n");
			messageBuffer.append(e.toString());
			logger.debug(messageBuffer.toString());
		}
	}

	public void info(Object message, Exception e)
	{
		if (logger.isInfoEnabled())
		{
			StringBuilder messageBuffer = new StringBuilder();
			messageBuffer.append(message);
			messageBuffer.append(getLocationInfo(e));
			messageBuffer.append("\n");
			messageBuffer.append(e.toString());
			logger.info(messageBuffer.toString());
		}
	}

	public void warn(Object message, Exception e)
	{
		if (logger.isEnabledFor(Level.WARN))
		{
			StringBuilder messageBuffer = new StringBuilder();
			messageBuffer.append(message);
			messageBuffer.append(getLocationInfo(e));
			messageBuffer.append("\n");
			messageBuffer.append(e.toString());
			logger.warn(messageBuffer.toString());
		}
	}

	public void error(Object message, Exception e)
	{
		if (logger.isEnabledFor(Level.ERROR))
		{
			StringBuilder messageBuffer = new StringBuilder();
			messageBuffer.append(message);
			messageBuffer.append(getLocationInfo(e));
			messageBuffer.append("\n");
			messageBuffer.append(e.toString());
			logger.error(messageBuffer.toString());
		}
	}

	public void fatal(Object message, Exception e)
	{
		if (logger.isEnabledFor(Level.FATAL))
		{
			StringBuilder messageBuffer = new StringBuilder();
			messageBuffer.append(message);
			messageBuffer.append(getLocationInfo(e));
			messageBuffer.append("\n");
			messageBuffer.append(e.toString());
			logger.fatal(messageBuffer.toString());
		}
	}

	private Logger logger;
}
